package OT.shoufouni.serverThreads;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import org.andnav.osm.util.GeoPoint;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlayItem;

import OT.shoufouni.gpsMaps.Map;
import OT.shoufouni.mainGUI.myLocation;
import android.util.Log;

public class serverLocateFriendsQuaryThread extends Thread {
   private final Map callerActivity;
   private final String idToLocate;
   public serverLocateFriendsQuaryThread(Map callerActivity, String name) {
      this.callerActivity = callerActivity;
      this.idToLocate = name;
    }
   public void run() {
	   ArrayList<myLocation>receivedData = httpGet(idToLocate);
	   ArrayList<OpenStreetMapViewOverlayItem> stations = null;
	   if (null!=receivedData){
		   stations = new ArrayList<OpenStreetMapViewOverlayItem>();
		   for (int i=0;i<receivedData.size();i++){
			   stations.add(new OpenStreetMapViewOverlayItem("friend n"+i,
					   receivedData.get(i).getName(),
					   new GeoPoint(receivedData.get(i).getX()
							   ,receivedData.get(i).getY())));
		   }
	   }
	   callerActivity.responseDisplay(stations);
   }
   private ArrayList<myLocation> httpGet(String idToLocate) {
	   String requestUrl;
	   StringBuilder builder = new StringBuilder();
	   ArrayList<myLocation> locations = new ArrayList<myLocation>();
       try {
           Log.d("serverLocateFriendsQuaryThread ",idToLocate);

    	   requestUrl = "http://10.0.2.2:8080/serverRestSide2/allFriends/"+idToLocate;
    	   try {
               URL url = new URL(requestUrl.toString());
               BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
               String inputLine = null;
               Log.d("serverLocateFriendsQuaryThread ","-----RESPONSE START-----" +" id="+ Thread.currentThread().getId());
               
               int i = 0;
               while ((inputLine = in.readLine()) != null) {
            	   if ("BAD".equals(inputLine)){
            		   return null;
            	   }
            	   String [] tempString=inputLine.split("\\,");
            	   if ((0<tempString.length)&&(3>tempString.length)){
            		   Log.d("serverLocateFriendsQuaryThread ","bad connection");
            		   //return locations;
            	   }
            	   locations.add(new myLocation(Double.parseDouble(tempString[0]),Double.parseDouble(tempString[1]),tempString[2]));
                   Log.d("serverLocateFriendsQuaryThread ",locations.get(i).getX()+","+locations.get(i).getY()+","+locations.get(i).getName());
                   ++i;

               }
               in.close();
               Log.d("serverLocateFriendsQuaryThread ",builder.toString());
               Log.d("serverLocateFriendsQuaryThread ","-----RESPONSE END-----" +" id="+ Thread.currentThread().getId());
               
               
           } catch (IOException e) {
        	   Log.e("serverLocateFriendsQuaryThread ",e.getMessage());
               return null;
           }
       }finally{
       }
       return locations;

   	}
   
}
